package com.iknight.mgr.core.jstree;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by JERY on 2017/7/18.
 */
public class JsTreeUtils {
    public static synchronized <T extends JsTreeNode> List<T> getRootNode(List<T> treeNodes) {
        List<T> newTreeNodes = new ArrayList<T>();
        for (T node : treeNodes) {
            if (node.getParent().equals("#")) {
                node.setChildren(getChildNodes(node.getId(), treeNodes));
                newTreeNodes.add(node);
            }
        }
        return newTreeNodes;
    }
    public static final <T extends JsTreeNode> List<T> getChildNodes(String parentId, List<T> treeNodes) {
        List<T> childNodes = new ArrayList<T>();
        for (T node : treeNodes) {
            if (null != node.getParent() && !node.getParent().equals("")) {
                if (node.getParent().equals(parentId)) {
                    node.setChildren(getChildNodes(node.getId(), treeNodes));
                    childNodes.add(node);
                }
            }
        }
        return childNodes;
    }
}
